package pres.liko.iot.sdk.service.impl.impl;

import io.github.pnoker.common.bean.driver.AttributeInfo;
import io.github.pnoker.common.bean.point.PointValue;
import io.github.pnoker.common.model.Device;
import io.github.pnoker.common.model.Point;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pres.liko.iot.sdk.bean.driver.DriverContext;
import pres.liko.iot.sdk.service.DriverCommandService;
import pres.liko.iot.sdk.service.DriverCustomService;
import pres.liko.iot.sdk.service.DriverService;

import java.util.Map;

/**
 * @ClassName: DriverCommandServiceImpl
 * @Auther: liko
 * @Description:
 * @Date: 2022/11/7 19:05
 */
@Service
public class DriverCommandServiceImpl implements DriverCommandService {

    @Autowired
    private DriverContext driverContext;
    @Autowired
    private DriverCustomService driverCustomService;
    @Autowired
    private DriverService driverService;

    /**
     * 通过设备及位号，读取基本信息
     *
     * @param deviceId
     * @param pointId
     * @return
     */
    @Override
    public PointValue read(String deviceId, String pointId) {
        //mqtt，qos代表不同的消息类型

        try {
            // 根据设备的id获取到设备的数据
            Device device = driverContext.getDeviceByDeviceId(deviceId);
            // 根据设备获取所属驱动,即所属驱动
            Map<String, AttributeInfo> driverInfo = driverContext.getDriverInfoByDeviceId(deviceId);
            // 根据驱动id和位号id获取到当前位号的一些属性
            Map<String, AttributeInfo> pointInfo = driverContext.getPointInfoByDeviceIdAndPointId(deviceId, pointId);
            Point point = driverContext.getPointByDeviceIdAndPointId(deviceId, pointId);

            // 根据不同的协议来从具体处理数据
            String rawString = driverCustomService.read(device, driverInfo, pointInfo, point);
            // 对位号的属性等数据进行转换：value就是转换后的数据，比如温度传感器获取到数据是华氏温度，需要转换为摄氏温度
            // 发送给设备
            return new PointValue(deviceId, pointId, rawString, driverService.convertValue(deviceId, pointId, rawString));
            // 将驱动协议加载到缓存
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public boolean write(String deviceId, String point, String value) {
        return false;
    }
}
